//基于地区选择的jquery 包装集的插件选择
(function($){
    $.fn.autoTime = function(optionss){
        var opts = $.extend({
            year:'',    //年
            month:'',   //月
            inpYear:'inpYear',     //当前表单年ID
            inpMonth:'inpMonth',    //当前表单月ID
            yearNum:20,      //显示年份个数
            established:'', //当前隐藏表单的值
            hiddenInp:'',        //表单默认值
            toDate:false
        },optionss||{}); //合并内容
        //直接指向this
        var othis = this;
        var myDate = new Date();
        var fullYear =  myDate.getFullYear();               //当前年份
        var startYear = '';         //开始年份
        var endYear = '';           //结束年份
        var hovering='';            //标记位
        var inpYearVal = '';        //年份val
        var inpMonthVal = '';       //月份val

        var hiddenInp =  (othis.find(opts.established).val()) ? othis.find(opts.established).val() : opts.hiddenInp;
        var established = hiddenInp.split('-');

        if(hiddenInp == '0000-00-00'){
            inpYearVal = 'to this day';
        }else if(established != ''){
            inpYearVal = established[0];
            inpMonthVal = established[1].replace(/(^[0]*)/g, "");
        }

        //年对象
        var objYear = $("<div id='"+opts.year+"' class='selTime'>" +
            "<span class='syear' id='syear'><input name='"+opts.inpYear+"' value='"+inpYearVal+"'  readonly type='text' id='"+opts.inpYear+"' placeholder='Select the year'></span><i></i></div>");
        //月对象
        if(opts.month != ''){
           var objMonth = $("<div id='"+opts.month+"' class='selTime selTimeMonth'>" +
               "<span class='syear' id='syear'><input name='"+opts.inpMonth+"' value='"+inpMonthVal+"' readonly type='text' id='"+opts.inpMonth+"' placeholder='month'></span><i></i></div>");
        }

        if(othis.find(opts.established).val() == '0000-00-00'){
            objYear.css({'background':'#e1e1e1'});
            objYear.find('input').css({'background':'#e1e1e1'});
            objMonth.css({'background':'#e1e1e1'});
            objMonth.find('input').css({'background':'#e1e1e1'});
        }

        setTime();
        function setTime(){
            //追加一级菜单
            othis.append(objYear);
            //追加二级菜单
            othis.append(objMonth);
            //经过设置经过菜单id标记位
            objYear.hover(function(){
                hovering=1;
            },function(){
                hovering='';
            });
            if(opts.month != '') {
                objMonth.hover(function () {
                    hovering = 2;
                }, function () {
                    hovering = '';
                });
            }

            //窗口事件
            $(window).resize(function(){
                ////判断对象是不是显示
                if(othis.find("#dateYear").is(':visible')) {
                    othis.find("#dateYear").css({
						left:0,
						top:objYear.height()+1

                        //"left": objYear.offset().left,
                        //"top": objYear.offset().top+objYear.height()+1
                    }).show();
                }
            });

            //窗口事件
            $(window).resize(function(){
                ////判断对象是不是显示
                if(othis.find("#dateMonth").is(':visible')) {
                    othis.find("#dateMonth").css({
						left:objMonth.offset().left-objYear.offset().left,
						top:objMonth.height()+1

                        //"left": objMonth.offset().left,
                        //"top": objMonth.offset().top+objMonth.height()+1
                    }).show();
                }
            });


            //年份点击事件
            objYear.click(function(){
                //点击当前年份其它隐藏
                if(othis.find("#dateYear").length  < 1){
                    //加载年份列表
                    setupOneNode();
                    //设置标记位
                    othis.find("#dateYear").hover(function(){
                        hovering=1;
                    },function(){
                        hovering='';
                    });

                    //当前最后一个值是不是大于当前年份
                    endYear = parseInt( othis.find("#dateYear").find('ul li a').eq(parseInt(opts.yearNum)-1).html() );
                    if(endYear  < fullYear){
                        othis.find("#dateYear").find('#next').show();
                    }

                    //点击上一步显示年份处理
                    othis.find("#dateYear").find('#pro i').click(function(){
                        endYear = parseInt( othis.find("#dateYear").find('ul li a').eq(0).html() );
                        startYear = endYear - parseInt(opts.yearNum);
                        var  str = '';
                        for(var i=startYear;i<endYear;i++){
                            str += '<li><a>'+i+'</a></li>';
                        }
                        if(opts.toDate){
                            str += '<li class="toDate"><a>to this day</a></li>';
                        }
                        othis.find("#dateYear").find('ul').html(str);
                        othis.find("#dateYear").find('#next').show();

                        liClick();
                    });

                    //点击下一步显示年份处理
                    othis.find("#dateYear").find('#next i').click(function(){
                        startYear = parseInt( othis.find("#dateYear").find('ul li a').eq(parseInt(opts.yearNum)-1).html() );
                        endYear = startYear + parseInt(opts.yearNum);
                        if(endYear >= fullYear){
                            othis.find("#dateYear").find('#next').hide();
                        }
                        //判断年份
                        endYear = (endYear >= fullYear) ? fullYear : endYear;
                        startYear = endYear - parseInt(opts.yearNum);
                        var  str = '';
                        for(var i=startYear+1;i<=endYear;i++){
                            str += '<li><a>'+i+'</a></li>';
                        }
                        if(opts.toDate){
                            str += '<li class="toDate"><a>to this day</a></li>';
                        }
                        othis.find("#dateYear").find('ul').html(str);
                        liClick();
                    });
                    //点击事件
                    liClick();

                    //点击其它地方隐常菜单
                    $(document).bind("click",function(){
                        if(hovering!=1){
                            othis.find("#dateYear").hide();
                        }
                    });

                }else{
                    othis.find("#dateYear").show();
                    if(othis.find("#dateYear").is(':visible')) {
                        othis.find("#dateYear").css({
							left:0,
							top:objYear.height()+1
                            //"left": objYear.offset().left,
                            //"top": objYear.offset().top+objYear.height()+1
                        }).show();
                    }
                }
            });
            if(opts.month != '') {
                //月份点击事件
                objMonth.click(function () {
                    objMonthClick();
                });
            }
        }

        //月份点击事件
        function objMonthClick(){
            //alert(hiddenInp)
            if(othis.find(opts.established).val() == '0000-00-00'){
                //禁止点击事件
                objMonth.unbind("click");
            }else{
                if(othis.find("#dateMonth").length  < 1){
                    setupTwoNode();
                    //点击月份其它隐藏设置标记位
                    othis.find("#dateMonth").hover(function(){
                        hovering=2;
                    },function(){
                        hovering='';
                    });
                    monthLiClick();
                    //点击其它地方隐常菜单
                    $(document).bind("click",function(){
                        if(hovering!=2){
                            othis.find("#dateMonth").hide();
                        }
                    });
                }else{
                    othis.find("#dateMonth").show();
                    if(othis.find("#dateMonth").is(':visible')) {
                        othis.find("#dateMonth").css({
							left:objMonth.offset().left-objYear.offset().left,
							top:objMonth.height()+1

                            //"left": objMonth.offset().left,
                            //"top": objMonth.offset().top+objMonth.height()+1
                        }).show();
                    }
                }
             }

        }

        //选择月点击事件
        function monthLiClick(){
            othis.find("#dateMonth").find('ul li a').each(function(){
                $(this).click(function(){
                    othis.find('#dateMonth').hide();
                    othis.find("#"+opts.inpMonth).val($(this).html());

                    //给隐藏表单赋值
                    if(othis.find('#inpYear').val() !='' ){
                        var establishedVal = '';
                        if($(this).html().length == '1'){
                            establishedVal =  othis.find('#inpYear').val() + '-0' + $(this).html() + '-00';
                        }else{
                            establishedVal =  othis.find('#inpYear').val() + '-' + $(this).html() + '-00';
                        }
                        othis.find(opts.established).val(establishedVal);
                    }
                });
            });
        }

        //选择年份点击事件
        function liClick(){
            othis.find("#dateYear").find('ul li a').each(function(){
                $(this).click(function(){
                    othis.find('#dateYear').hide();
                    othis.find("#"+opts.inpYear).val($(this).html());
                    if($(this).html() == '至今'){
                        othis.find(opts.established).val('0000-00-00');
                        othis.find(objYear).css({'background':'#e1e1e1'});
                        othis.find(objYear).find('input').css({'background':'#e1e1e1'});

                        othis.find(objMonth).css({'background':'#e1e1e1'});
                        othis.find(objMonth).find('input').css({'background':'#e1e1e1'}).val('');
                        //禁止点击事件
                        othis.find(objMonth).unbind("click");
                    }else{
                        othis.find(objYear).css({'background':'#fff'});
                        othis.find(objYear).find('input').css({'background':'#fff'});

                        othis.find(objMonth).css({'background':'#fff'});
                        othis.find(objMonth).find('input').css({'background':'#fff'});
                        //移除点击事件
                        othis.find(objMonth).die().on("click",function(){
                            //事件运行代码
                            if(othis.find("#dateMonth").length  < 1){
                                setupTwoNode();
                                //点击月份其它隐藏设置标记位
                                othis.find("#dateMonth").hover(function(){
                                    hovering=2;
                                },function(){
                                    hovering='';
                                });
                                monthLiClick();
                                //点击其它地方隐常菜单
                                $(document).bind("click",function(){
                                    if(hovering!=2){
                                        othis.find("#dateMonth").hide();
                                    }
                                });
                            }else{
                                othis.find("#dateMonth").show();
                                if(othis.find("#dateMonth").is(':visible')) {
                                    othis.find("#dateMonth").css({
										left:objMonth.offset().left-objYear.offset().left,
										top:objMonth.height()+1

                                        //"left": objMonth.offset().left,
                                        //"top": objMonth.offset().top+objMonth.height()+1
                                    }).show();
                                }
                            }
                        });

                        //给隐藏表单赋值
                        if(othis.find('#inpMonth').val() ==''){
                            othis.find(opts.established).val($(this).html() + '-00-00');
                        }else{
                            var establishedVal = '';
                            if(opts.month != '') {
                                if (othis.find('#inpMonth').val().length == '1') {
                                    establishedVal = $(this).html() + '-0' + othis.find('#inpMonth').val() + '-00';
                                } else {
                                    establishedVal = $(this).html() + '-' + othis.find('#inpMonth').val() + '-00';
                                }
                            }
                            othis.find(opts.established).val(establishedVal);
                        }
                        //是不是为空月份
                        if(othis.find("#"+opts.inpMonth).val () == ''){
                            if(othis.find("#dateMonth").length  < 1){
                                setupTwoNode();
                                monthLiClick();
                                //点击其它地方隐常菜单
                                hovering = 2;
                                $(document).bind("click",function(){
                                    if(hovering!=2){
                                        othis.find("#dateMonth").hide();
                                    }
                                });
                            }else{
                                othis.find("#dateMonth").show();
                                monthLiClick();
                                hovering = 2;
                            }
                        }
                    }
                    //获得证书为空时
                    if(opts.month == '') {
                        othis.find(opts.established).val($(this).html()+ '-00-00');
                    }

                });
            });
        }

        /**加载一级菜单函数
         * return 年份列表
         */
        function setupOneNode(){
            var inpYear = othis.find("#"+opts.inpYear).val();  //获取当前表单的年份值转化为整形值
            if(inpYear == 'to this day'){
                inpYear ='';
            }
            var str = '';
            str += '<div class="dateChange" id="dateYear"><div class="dateCon" id="dateCon">';
            str += '<span class="pro" id="pro"><a><i></i></a></span>';
            str += '<ul>';
            //当表单的input值为空时
            if(inpYear == ''){
                for(var i=(fullYear-opts.yearNum)+1;i<=fullYear;i++){
                    str += '<li><a>'+i+'</a></li>';
                }
                if(opts.toDate){
                    str += '<li class="toDate"><a>to this day</a></li>';
                }
            }else{
                inpYear = parseInt(inpYear);    //表单值转化为整形值
                //当前年份和当前表单的值小于总数显示的一半数
                if(fullYear - inpYear < Math.floor(opts.yearNum/2)){
                    //获取开始年份
                    startYear = inpYear - (opts.yearNum - ( fullYear - inpYear ));
                    for(var i=startYear+1;i<=fullYear;i++){
                        str += '<li><a>'+i+'</a></li>';
                    }
                }else{
                    //获取开始年份和结束年份
                    startYear = inpYear - Math.floor(opts.yearNum/2);
                    endYear = inpYear + Math.floor(opts.yearNum/2);
                    for(var i=startYear;i<endYear;i++){
                        str += '<li><a>'+i+'</a></li>';
                    }
                }
                if(opts.toDate){
                    str += '<li class="toDate"><a>to this day</a></li>';
                }
            }
            str += '</ul>';
            str += '<span class="next" id="next"><a><i></i></a></a></span>';
            str += '</div></div>';

            objYear.after(str);

            othis.find("#dateYear").css({
                left:0,
				top:objYear.height()+1
				//left:objYear.offset().left,
				//top:objYear.offset().top+objYear.height()+1
            }).show();
        }

        //加载二级菜单函数
        // return 月份列表
        function setupTwoNode(){
            var str = '';
            str += '<div class="dateChange" id="dateMonth"><div class="dateMonthCon" id="dateMonthCon">';
            str += '<ul>';
            for(var i=1;i<=12;i++){
                str += '<li><a>'+i+'</a></li>';
            }
            str += '</ul>';
            str += '</div></div>';

            objMonth.after(str);
            othis.find("#dateMonth").css({

				left:objMonth.offset().left-objYear.offset().left,
				top:objMonth.height()+1

                //left:objMonth.offset().left,
                //top:objMonth.offset().top+objMonth.height()+1
            }).show();
        }
        return this;
    };
})(jQuery);